package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import database.Conn;

public class Payment {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblPayment";
	public boolean AddPayment(int id_customer,int price_r,int charge_sv,int total,int payment,int date,int status){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_customer,price_r,charge_s,total,payment,date,status) VALUES (?,?,?,?,?,?,?)");
			pstm.setInt(1,id_customer);
			pstm.setInt(2,price_r);
			pstm.setInt(3,charge_sv);
			pstm.setInt(4,total);
			pstm.setInt(5,payment);
			pstm.setInt(6,date);
			pstm.setInt(7,status);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeletePayment(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdatePayment(int id,int id_customer,int price_r,int charge_sv,int total,int payment,int date,int status){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET id_customer=?,price_r=?,charge_s=?,total=?,payment=?" +
					"date=?,status=?  WHERE id=?");
			pstm.setInt(1,id_customer);
			pstm.setInt(2,price_r);
			pstm.setInt(3,charge_sv);
			pstm.setInt(4,total);
			pstm.setInt(5,payment);
			pstm.setInt(6,date);
			pstm.setInt(7,status);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Payment> GetPayment(){
		connect= Conn.getConnect();
		ArrayList<entity.Payment> list= new ArrayList<entity.Payment>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.Payment payment = new entity.Payment();
				payment.setId_customer(rs.getInt("id_customer"));
				payment.setPrice_r(rs.getInt("price_r"));
				payment.setCharge_sv(rs.getInt("charge_s"));
				payment.setTotal(rs.getInt("total"));
				payment.setPayment(rs.getInt("payment"));
				payment.setDate(rs.getInt("date"));
				payment.setStatus(rs.getInt("status"));
				list.add(payment);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetPaymentById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
	// Lay thong tin gui den controller
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
